<?
	/* View */
	define('PS', 1);
	include_once("../../defines.php");
	include_once('header.php');
	
	$PageName = 'news';
	include_once(PUBLIC_BEHIND_DIRECTORY . $PageName . '.php');
?>
<div id="fb-root"></div>
<script type="text/javascript" src="<?php echo PUBLIC_JS_PATH; ?>jquery.cycle.all.js"></script>

<script type="text/javascript">

	$("#newsVideos")
	.after('<div id="menuVideos" class="menuResources">')
	.cycle({ 
	    fx:     'turnDown', 
	    speed:  'fast', 
	    timeout: 0, 
	    pager:  '#menuVideos' 
	});

	/*$("#newsAudios")
	.after('<div id="menuAudios" class="menuResources">')
	.cycle({ 
	    fx:     'turnDown', 
	    speed:  'fast', 
	    timeout: 0, 
	    pager:  '#menuAudios' 
	});*/

	var paginator = {
		currentPage : 1,
		firstPage : 1,
		lastPage : <?=$TotalPages;?>,
		commentsPerPage : <?= COMMENTS_PER_PAGE ?>
	};
	var defaultText = "Ingrese el texto...";

	var bringNewComments = function (paginator){
		$.ajax({
			url : '<?=PUBLIC_CONTROLLERS_PATH . "load_comments.php" ?>',
			data : { currentPage : (paginator.currentPage - 1), news_id : <?=$_REQUEST["id"];?>, operation : "read" },
			type : "post",
			dataType : "html",
			success : function(comments){
				comments = JSON.parse(comments);
				totalComments = comments.totalComments;
				comments = comments.text;
				
				$("#ATTCommentsDisplay").html(comments);
				createPaginatorController();
				$("#totalOfComments").html(totalComments);
				$(".initialFinalComments #initial").html(paginator.commentsPerPage*(paginator.currentPage - 1) + 1);
				finalValue = paginator.commentsPerPage*paginator.currentPage < totalComments ? paginator.commentsPerPage*paginator.currentPage : totalComments;
				$(".initialFinalComments #final").html(finalValue);
				$("#page_" + paginator.currentPage).addClass('selected');
				if (totalComments <= paginator.commentsPerPage){
					 $("#firstPage").css("display", "none");
					 $("#lastPage").css("display", "none");
					 $("#previousPage").css("display", "none");
					 $("#nextPage").css("display", "none");
				}else{
					 $("#firstPage").css("display", "inline-block");
					 $("#lastPage").css("display", "inline-block");
					 $("#previousPage").css("display", "inline-block");
					 $("#nextPage").css("display", "inline-block");
				}
				$(".commentDisplay").css("display", "block");
			}
		});
	};

	var createPagerNumbers = function(){
		pagesToShow = [];

		if (paginator.lastPage  <= 3){
			for (a = 1; a <= paginator.lastPage; a++){
				pagesToShow.push(a);
			}
		}else{
			if (paginator.currentPage == paginator.lastPage)
				pagesToShow = [paginator.currentPage - 2, paginator.currentPage - 1 , paginator.currentPage];
			else if (paginator.currentPage == paginator.firstPage)
				pagesToShow = [paginator.currentPage, paginator.currentPage + 1 , paginator.currentPage + 2];
			else
				pagesToShow = [paginator.currentPage - 1, paginator.currentPage, paginator.currentPage + 1];
		}

		var pagerHTML = "";
		if (pagesToShow.length == 1)
			return;
		$.each(pagesToShow, function(index, pageNumber){
			selected = "";
			if (paginator.currentPage == pageNumber)
				selected = "selected";

			pagerHTML += "<div class='page_container inlineBlock'><a href='javascript: void(0);' class='page " + selected + "' id='page_" + pageNumber + "' data-page='" + pageNumber + "' ";
			if (pageNumber == paginator.currentPage)
				pagerHTML += "class='current' ";
			 pagerHTML += ">" + pageNumber + "</a></div>";
		});
		
		$("#nearestPages").html(pagerHTML);
	};
	
	var createPaginatorController = function(){
		if (paginator.lastPage == 0){
			$("#ATTcomentsPager").css("display", "none");
			return;
		}else{
			$("#ATTcomentsPager").css("display", "inline");
		}
		
		createPagerNumbers();
		if (paginator.currentPage == paginator.firstPage)
			$("#firstPage, #previousPage").css("display", "none");
		else
			$("#firstPage, #previousPage").css("display", "inline");

		if (paginator.currentPage == paginator.lastPage)
			$("#lastPage, #nextPage").css("display", "none");
		else
			$("#lastPage, #nextPage").css("display", "inline");
	};

	var publishNewComment = function (){		
		$.ajax({
			url : '<?=PUBLIC_CONTROLLERS_PATH . "load_comments.php" ?>',
			data : $("#frmContact").serialize(),
			type : "post",
			dataType : "html",
			success : function(){
				$.unblockUI();
				alert("Gracias por comentar!");
				$(".resetAfterPublish").val(defaultText);
				bringNewComments(paginator);
			}
		});
		$.blockUI({message : '<div class="sendingMessage">Enviando...<img src="<?=PUBLIC_IMAGES_PATH . "sending.gif";?>" alt="Sending"/></div>'});
	};


	$(function(){
		$("#firstPage").click(function(){
			paginator.currentPage = paginator.firstPage;
			bringNewComments(paginator);
		});

		$(".page").live('click', function(){
			page = $(this).data("page");
			if (page)
				paginator.currentPage = parseInt(page);
			bringNewComments(paginator);
		});

		$("#lastPage").click(function(){
			paginator.currentPage = paginator.lastPage;
			bringNewComments(paginator);
		});

		$("#previousPage").click(function(){
			if (paginator.currentPage > paginator.firstPage)
				paginator.currentPage--;
			bringNewComments(paginator);
		});

		$("#nextPage").click(function(){
			if (paginator.currentPage < paginator.lastPage)
				paginator.currentPage++;
			bringNewComments(paginator);
		});

		$("#btnAccept").click(function(){
			functions.validateAllCompleted();
			if (functions.errorExists())
				return false;
			publishNewComment();

			$("#formContent input[type=text], #formContent textarea").addClass("emptyText");
		});

		$(".contactText, textarea").click(function(){
			functions.setEmptyText(this, defaultText);
		});

		$(".contactText, textarea").focusin(function(){
			functions.setEmptyText(this, defaultText);
		});

		$(".contactText, textarea").focusout(function(){
			count = functions.trim($(this).val()).length;
			if (count == 0){
				$(this).addClass('emptyText');
				$(this).val(defaultText);
			}
		});

		$(".contactText, textarea").keypress(function(){
			if ($(this).val() != defaultText){
				$(this).removeClass('error');
				$(this).removeClass('emptyText');
			}
		});

		$("#commentsTab").click(function(){
			$(this).addClass('selected');
			$("#fbkTab").removeClass('selected');

			$("#ATTComments").removeClass("hidden");
			$("#fbkComments").addClass("hidden");
		});

		$("#fbkTab").click(function(){
			$(this).addClass('selected');
			$("#commentsTab").removeClass('selected');

			$("#fbkComments").removeClass("hidden");
			$("#ATTComments").addClass("hidden");
		});
		
		$.each($(".contactText.emptyText, textarea.emptyText"), function(){
			$(this).val(defaultText);
		});

		createPaginatorController();
	});
	
</script>

<div id="newsContent" class="centerContent">
	<h1><?= $News->getTitle();?></h1>
	<h3><?= $News->getSubtitle();?></h3>
	
	<?php
	if ($HasImages == 1){
	?>
	<div>
		<div class="cycle-slideshow" data-cycle-fx="shuffle" data-cycle-timeout="2000">
		<? 
			foreach($NewsImages as $Image){
				if (empty($Image["tag"]))
					echo '<img src="' . MAIN_IMAGES_NEWS_PATH . $Image["name_file"] . '" alt="' . $Image["text"] . '" title="' . $Image["text"] . '" />';
			}
		?>
		</div>
	</div>
	<?
	}
	?>
	
	<div><?= stripslashes(FunctionsHelper::DiscardPre($News->getText()));?></div>

	<?
	if (count($NewsVideos) > 0){
	?>
	<div id="newsVideosContainer" class="resource videoPosition">
		<div id="newsVideos">
	<? 
		foreach($NewsVideos as $Video){
			echo '<iframe class="videoPosition" src="http://www.youtube.com/embed/' . $Video["link"] . '" frameborder="0" allowfullscreen></iframe>';
		}
	?>
		</div>
	</div>
	<?
	}

	if (count($NewsAudios) > 0){
	?>
	<div id="newsAudiosContainer" class="resource">
		<div id="newsAudios">
	<? 
		foreach($NewsAudios as $Audio){
			/*echo '
				<audio controls="controls">
					<source src="' . MAIN_FILES_PATH . $Audio["filename"] . '" type="audio/ogg" />
				</audio>';
			*/
			echo '<embed src="' . MAIN_FILES_PATH . $Audio["name_file"] . '" volume="50" height="60" width="420" autostart="false" loop="false" style="height: 60px;" />';
		}
	?>
		</div>
	</div>
	<?
	}
	?>
	<div id="tagsDiv">
		<span class="miniSize bold">Tags:</span>
		<?
		if (count($NewsTags) > 0){
			$i = 0;
			foreach ($NewsTags as $Tag){
				$i++;
				echo $Tag["tag"];
				if ($i < count($NewsTags))
					echo ', ';
				else
					echo '.';
			}
		}else{
			echo $messages->getMessage("NotTagsToShow");
		}
		?>
	</div>
	
	
	
	
	<div id="disqus_thread"></div>
    <script type="text/javascript">
        /* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
        var disqus_shortname = 'atodotenis'; // required: replace example with your forum shortname

        /* * * DON'T EDIT BELOW THIS LINE * * */
        (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
        })();
    </script>
    <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
    <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
    
	
	
	

	<!-- 
		<script type="text/javascript">
			window.fbAsyncInit = function() {
				FB.init({
				  appId      : '315171441913057', // App ID
				  channelUrl : 'http://localhost/channel.html', // Channel File
				  status     : true, // check login status
				  cookie     : true, // enable cookies to allow the server to access the session
				  xfbml      : true  // parse XFBML
				});
			}
			FB.XFBML.parse();
		</script>
		<div id="comments">
			<div class="fb-comments" data-href="http://localhost/atodotenis" data-num-posts="10" data-width="882"></div>
		</div>
	 -->
</div>
<? include('footer.php'); ?>